package com.wcfsy.wechat.wx.pagination.impl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.wcfsy.wechat.wx.pagination.IPage;

public class Page<T> implements IPage<T> {
	private int size;
	private int index;
	private long total;
	private long totalRecord;
	private List<T> data;
	private Map<String,Object> map;
	
	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	public Page() {
	}

	public Page(int size, int index) {
		this.size = size;
		this.index = index;
	}

	public Page(List<T> data, long totalRecord, int index, int size) {
		this.data = (data == null ? new ArrayList<T>(0) : data);
		this.totalRecord = totalRecord;
		this.size = size;
		this.index = index;
		this.total = ((totalRecord - 1L) / size + 1L);
	}

	public void setTotalRecord(long totalRecord) {
		this.totalRecord = totalRecord;
	}

	public long getTotalRecord() {
		return this.totalRecord;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getSize() {
		return this.size;
	}

	public long getTotal() {
		return this.total;
	}

	public int getIndex() {
		return this.index;
	}

	public List<T> getData() {
		return this.data;
	}

	public String toString() {
		StringBuilder buf = new StringBuilder();
		Method[] methods = getClass().getMethods();
		boolean isFirst = true;
		int i = 0;
		for (int n = methods.length; i < n; i++) {
			try {
				Method method = methods[i];
				if (((method.getModifiers() & 0x1) != 1)
						|| (method.getDeclaringClass() == Object.class)
						|| ((method.getParameterTypes() != null) && (method
								.getParameterTypes().length != 0)))
					continue;
				String methodName = method.getName();
				String property = null;
				if (methodName.startsWith("get"))
					property = methodName.substring(3, 4).toLowerCase()
							+ methodName.substring(4);
				else if (methodName.startsWith("is")) {
					property = methodName.substring(2, 3).toLowerCase()
							+ methodName.substring(3);
				}
				if (property != null) {
					Object value = method.invoke(this, new Object[0]);
					if (isFirst)
						isFirst = false;
					else
						buf.append(",");
					buf.append(property);
					buf.append(":");
					if ((value instanceof String))
						buf.append("\"");
					buf.append(value);
					if ((value instanceof String))
						buf.append("\"");
				}
			} catch (Exception localException) {
			}
		}
		return "{" + buf.toString() + "}";
	}
}